﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Http;
using DBHelper;

namespace Grade
{
    public partial class frmGrade : Form
    {
        
        private List<string> urlItem = new List<string>();
        private int count = 0;          //当前指针
        private bool is_Sucess = true;

        public frmGrade()
        {
            InitializeComponent();            
            BindUrlItem();  //绑定网址
        }

        //网站评分完毕执行的事件
        private void request_On_Sucecc(Colligate colligate)
        {
            Insert(colligate);  //评分结果加入数据库
            //LoadUrl();          //加载一个新的Url = [出现递归调用]
            is_Sucess = true;
        }

        //开始请求
        private void btn_Start_Click(object sender, EventArgs e)
        {
            btn_Start.Enabled = true;
            txt_State.Text = "任务加载中...";
            time_Coll.Start();
        }

        /// <summary>
        /// 绑定网址库列表
        /// </summary>
        private void BindUrlItem()
        {
            string getAllUrl = "select url from t_url";
            DataTable dt = new DataTable();
            using (DBase db = new DBase())
            {
                dt = db.GetDataTable(getAllUrl);
            }
            if (dt != null && dt.Rows.Count != 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    urlItem.Add(dt.Rows[i][0].ToString());
                }
            }
        }

        /// <summary>
        /// 加载一个Url
        /// </summary>
        private void LoadUrl()
        {
            if (count < urlItem.Count)
            {
                
                string src = urlItem[count].ToString();
                txt_State.Text = string.Format("{0}/{1} 请求:{2}",
                    (count+1).ToString(),
                    urlItem.Count,
                    src);
                count++;
                HttpRequesst request = new HttpRequesst();
                request.On_Sucecc += new On_GradeSucess(request_On_Sucecc);
                request.Requesst(src);
            }
            else
            {
                txt_State.Text = "全部执行完毕...";
                btn_Start.Enabled = true;   //按钮可用
                time_Coll.Stop();
            }
        }

        /// <summary>
        /// 修改数据库评分结果
        /// </summary>
        /// <param name="col">评分结果结构</param>
        private void Insert(Colligate col)
        {
            string up = string.Format("update t_url set content={0},speed={1},httpCode={2},pageSize={3},abetHtml={4},googleEmbody={5},pr={6},baiduEmbody={7},destoon={8} where url='{9}'",
                col.Content.ToString(),
                col.Speed.ToString(),
                col.HttpCode.ToString(),
                col.PageSize.ToString(),
                col.AbetHtml.ToString(),
                col.GoogleEmbody.ToString(),
                col.GooglePR.ToString(),
                col.BaiduEmbody.ToString(),
                col.Destoon.ToString(),
                col.Domain);
            using (DBase db = new DBase())
            {
                db.GetDataTable(up);
            }
        }

        private void time_Coll_Tick(object sender, EventArgs e)
        {
            if (is_Sucess)
            {
                is_Sucess = false;
                LoadUrl();  //加载Url
            }
        }

        private void frmGrade_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}